set.seed(853)
newborn_weight <- tibble(
weight = rep(rnorm(n = 1000, mean = 3.5, sd = 0.5), times = 3),
measurement = rep(c("Actual", "Censored", "Truncated"), each = 1000)
)
newborn_weight <- newborn_weight |>
mutate(weight = case_when(
weight <= 2.75 & measurement == "Censored" ~ 2.75,
weight >= 4.25 & measurement == "Truncated" ~ NA_real_,
TRUE ~ weight
))
newborn_weight |>
ggplot(aes(x = weight)) +
geom_histogram(bins = 50, fill = "steelblue", alpha = 0.7) +
facet_wrap(vars(measurement)) +
theme_minimal() +
labs(x = "Newborn Weight (kg)", y = "Count")